﻿namespace Dissemination.MVC.Controllers
{
    using System.Web.Mvc;
    using Dissemination.MVC.Repositories;
    using Dissemination.MVC.Models;

    using System;
    
    public class SurveyResultController : ControllerBase
    {
        protected SurveyResultRepository repository = new SurveyResultRepository();

        #region Details

        //
        // GET: /SurveyResult/Details/5

        /// <summary>
        /// Returns the view associated to the details of an entity in the data source.
        /// </summary>
        /// <param name="id">The entity id.</param>
        /// <returns>Details view</returns>
        [Authorize(Roles = "Reporter,Demostrator,Administrator")]
        public ActionResult Details(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering SurveyResult details with id={0}...", id);
            SurveyResult surveyResult = repository.Get(id);
            
            if (surveyResult == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("SurveyResult with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving SurveyResult details with id={0}...", id);
            return View(surveyResult);
        }

        #endregion

        #region Edit

        //
        // GET: /SurveyResult/Edit/5

        /// <summary>
        /// Returns the view associated with the edit of an existing entity.
        /// </summary>
        /// <param name="id">The id of the entity to edit</param>
        /// <returns>Edit view</returns>
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Edit(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering SurveyResult with id {0} edit (GET)...", id);
            SurveyResult surveyResult = repository.Get(id);

            if (surveyResult == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("SurveyResult with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving SurveyResult with id {0} edit (GET)...", id);
            return View(surveyResult);
        }

        //
        // POST: /SurveyResult/Edit/5

        /// <summary>
        /// Edits the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity.</param>
        /// <param name="collection">The collection of values entered.</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Reporter,Administrator")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering SurveyResult with id {0} edit (POST)...", id);
            SurveyResult surveyResult = repository.Get(id);
            
            try
            {
                UpdateModel(surveyResult);
                surveyResult.SurveyResultsFilled = true;
                repository.Save();

                if (log.IsDebugEnabled) log.DebugFormat("Leaving SurveyResult with id {0} edit (POST)...", id);
                return RedirectToAction("IndexOrDetails", "ReporterManager", new { targetAreaID = surveyResult.TargetArea.TargetAreaID });
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled) log.Error("Error editing SurveyResult", ex);
                //ModelState.AddRuleViolations(SurveyResult.GetRuleViolations());
                return View(surveyResult);
            }
        }

        #endregion

    }
}
